package manager.cms.service.exec;

import common.SnowflakeIdWorker;
import manager.cms.dao.CmsContentCatDao;
import manager.cms.dao.CmsContentDao;
import manager.cms.service.JdExecutor;
import manager.cms.vo.CmsContentCatVO;
import manager.cms.vo.CmsContentVO;
import manager.feign.ShopIndexFloorThirdService;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service(value = "dailySpeciTabExec")
public class DailySpeciTabExecImpl implements JdExecutor {
    @Autowired
    private ShopIndexFloorThirdService shopIndexFloorThirdService;
    @Autowired
    private CmsContentCatDao cmsContentCatDao;
    @Autowired
    private CmsContentDao cmsContenDao;
    @Override
    public void execute(Map<String,Object> mapParams) {
        try {
            JSONObject jsonObject = null;
            String  dailySpeciTab=shopIndexFloorThirdService.getDailySpeciTab();
            dailySpeciTab=dailySpeciTab.substring(18,dailySpeciTab.length()-1);
            jsonObject = new JSONObject(dailySpeciTab);
            JSONArray tabData=(JSONArray)jsonObject.get("data");
            for(int i=0;i<tabData.length();i++){
                JSONObject item=(JSONObject)tabData.get(i);
                CmsContentCatVO cmsContentCatVO=new CmsContentCatVO();
                JSONObject dailySpeciDataJson = null;
                List<CmsContentVO> contentVOList=new ArrayList<CmsContentVO>();
                String dailySpeciData = shopIndexFloorThirdService.getDailySpeciData(item.get("id").toString());
                String dailySpeciDataStr=dailySpeciData.substring(26,dailySpeciData.length()-1);
                dailySpeciDataJson = new JSONObject(dailySpeciDataStr);
                JSONArray todayMonetizeData=(JSONArray)dailySpeciDataJson.get("data");
                String dailySpeciDataContentId=String.valueOf(SnowflakeIdWorker.nextId());
                cmsContentCatVO.setName("特价商品"+"-->"+item.get("tabText"));
                cmsContentCatVO.setId(dailySpeciDataContentId);
                cmsContentCatDao.addCmsContentCat(cmsContentCatVO);
                for(int j=0;j<todayMonetizeData.length();j++){
                    CmsContentVO cmsContentVO=new CmsContentVO();
                    String id=String.valueOf(SnowflakeIdWorker.nextId());
                    JSONObject itemData=(JSONObject)todayMonetizeData.get(j);
                    cmsContentVO.setPic(itemData.get("imageurl").toString());
                    cmsContentVO.setTitle(itemData.get("wname").toString());
                    cmsContentVO.setId(id);
                    cmsContentVO.setCategoryId(dailySpeciDataContentId);
                    contentVOList.add(cmsContentVO);
                }
                cmsContenDao.addCmsContentBatch(contentVOList);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
